package com.meida.module.bus.provider.handler;

import cn.hutool.core.util.ObjectUtil;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.admin.client.entity.BaseUser;
import com.meida.module.admin.provider.service.BaseUserService;
import com.meida.module.bus.client.entity.BusMaterialPackage100;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusVersion;
import com.meida.module.bus.provider.service.BusVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description 100分包首页
 */
@Component("adminPackage100IndexPageListHandler")
public class AdminPackage100IndexPageListHandler implements PageInterceptor<BusMaterialPackage100> {
    @Autowired
    private BusVersionService busVersionService;
    @Autowired
    private BaseUserService baseUserService;

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        cq.addSelect("bmp.*");
        cq.addSelect("bp.projectCode,bp.projectName");
        cq.like("bp.projectCode",params.get("projectCode"));
        cq.like("bp.projectName",params.get("projectName"));
        cq.like("bmp.majorCode",params.get("majorCode"));
        cq.like("bmp.majorName",params.get("majorName"));
        cq.like("bmp.packageNo",params.get("packageNo"));
        cq.like("bmp.packageName",params.get("packageName"));
        cq.groupBy("bmp.projectId,bmp.majorCode,bmp.packageNo");
        //cq.orderByDesc("bmp.createTime");
        cq.createJoin(BusProject.class).setJoinAlias("bp").setJoinField("projectId").setMainField("projectId");
    }

    @Override
    public void complete(CriteriaQuery<BusMaterialPackage100>  cq, List<EntityMap> result, EntityMap extra) {
        if (FlymeUtils.isNotEmpty(result)) {
            for (EntityMap entityMap : result) {
                Long projectId = entityMap.getLong("projectId");
                EntityMap versionMap = busVersionService.selectLatestVersion(projectId,2);
                Long userId = null;
                if(ObjectUtil.isNotNull(versionMap)){
                    userId = versionMap.get("userId");
                    entityMap.put("versionNo",versionMap.get("bigVersion")+"."+versionMap.get("smallVerion"));
                    entityMap.put("versionTime",versionMap.get("updateTime"));
                }else {
                    entityMap.put("versionNo","");
                    entityMap.put("versionTime","");
                }
                if(userId != null){
                    BaseUser baseUser = baseUserService.getUserById(userId);
                    entityMap.put("userName",baseUser.getNickName());
                }else{
                    entityMap.put("userName","");
                }
            }
        }
    }

}
